import socket  # 导入 socket 模块
import time
# 设置服务器ip地址，端口，buffer容量
HOST = '192.168.1.105'
# HOST = socket.gethostname()
PORT = 8008
BUFFER = 4096

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  # 创建 socket 对象
s.bind((HOST, PORT))  # 绑定端口

s.listen(5)  # 等待客户端连接
while True:
    c, addr = s.accept()  # 建立客户端连接
    print('连接地址：', addr)
    while c:
        # while not rospy.is_shutdown():
        try:
            c.settimeout(5)
            result = c.recv(1)
            result = result.decode('utf-8')
            # pub.publish(result)
            print(result)
            time.sleep(0.001)
            if result == "p":
                print("stop")
                break
        except socket.timeout:
            # pub.publish("time out")
            print("time out")
# c.close()
